
sampler baseTexture : register(s0);

Texture frameTex;

sampler2D frameSampler = sampler_state
{
	Texture = <frameTex>;
    ADDRESSU = CLAMP;
	ADDRESSV = CLAMP;
	MAGFILTER = LINEAR;
	MINFILTER = LINEAR;
	MIPFILTER = LINEAR;
};

float4x4 View;
float4x4 WorldViewProjection;


half Decay = 0.5f;
half Exposition = 0.5f;

half3 LightPosition;
half3 LightDirection = {1.0f,1.0f,1.0f};
half3 CameraPos;


half4 PixelShaderFunction(float2 texCoord : TEXCOORD0) : COLOR0
{
    half4 screenPos = mul(LightPosition, WorldViewProjection); 
    half2 ssPos = screenPos.xy / screenPos.w 
			* float2(0.5,-0.5) + 0.5;
			
	half2 oriTexCoord = texCoord;
			
	half2 deltaTexCoord = (texCoord - ssPos);    
  
    half3 color = tex2D(baseTexture, texCoord);    
		
	texCoord -= deltaTexCoord;
			
	half3 sample = tex2D(baseTexture, texCoord);
	sample *= Decay;
	color += sample;			

	
	half amount = dot(mul(LightDirection,View), half3(0.0f,0.0f,-1.f));
	half4 sampleFrame = tex2D(frameSampler, oriTexCoord);
    
    return saturate(amount-0.5) * half4(color * Exposition, 1) + sampleFrame;
}


technique Scatter
{
    pass Pass1
    {
		PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}